//
//  APHomeHeaderView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/5/6.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "APHomeHeaderView.h"
#import "APViewModel.h"

@implementation APHomeHeaderView

+ (CGFloat)viewHeight {
    
    return 100;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [APViewModel themeColor];
        
        CGFloat subWidth = self.width/4;
        
        UIView *scan = [self subItemWithFrame:CGRectMake(0, 0, subWidth, self.height) image:[UIImage imageNamed:@"scan"] title:@"scan"];
        [self addSubview:scan];
        
        UIView *payment = [self subItemWithFrame:CGRectMake(scan.right, 0, subWidth, self.height) image:[UIImage imageNamed:@"payment"] title:@"payment"];
        [self addSubview:payment];
        
        UIView *collect = [self subItemWithFrame:CGRectMake(payment.right, 0, subWidth, self.height) image:[UIImage imageNamed:@"collect"] title:@"collect"];
        [self addSubview:collect];
        
        UIView *phone = [self subItemWithFrame:CGRectMake(collect.right, 0, subWidth, self.height) image:[UIImage imageNamed:@"phone"] title:@"phone"];
        [self addSubview:phone];
    }
    return self;
}

- (UIView *)subItemWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title {
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    
    CGFloat iconWidth = 44;
    CGFloat margin = 10;
    CGFloat labelHeight = 20;
    CGFloat top = (view.height-iconWidth-margin-labelHeight)/2;
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    iconView.frame = CGRectMake((view.width-iconWidth)/2, top, iconWidth, iconWidth);
    [view addSubview:iconView];
    
    UILabel *label = [Utils genLabelWithFrame:CGRectMake(0, iconView.bottom+margin, view.width, labelHeight) textAlign:NSTextAlignmentCenter fontSize:15 textColor:[UIColor whiteColor]];
    label.text = title;
    [view addSubview:label];
    
    return view;
}

#pragma mark - Set方法
-(void)setContentOffsetY:(CGFloat)contentOffsetY {
    
    _contentOffsetY = contentOffsetY;
    
    CGFloat alpha = 0;
    if (contentOffsetY > 90) {
        alpha = 0;
    } else if (contentOffsetY > 20) {
        alpha = (90 - contentOffsetY) / 50;
    } else {
        alpha = 1;
    }
    
    for (UIView *subview in self.subviews) {
        subview.alpha = alpha;
    }
}

@end
